<?php
declare (strict_types = 1);

namespace app\admin\controller;

use app\admin\model\ConfigFieldModel;
use app\admin\service\ConfigService;
use app\Request;
use think\facade\View;

class Config extends AdminController
{
    /**
     * 基础配置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-15
     * @return void
     */
    public function index(Request $request){
        if($request->isPost()){
            $data = [
                'sitename' => $request->param('sitename'),
                'siteurl' => $request->param('siteurl'),
                'siteinfo' => $request->param('siteinfo'),
                'sitekeywords' => $request->param('sitekeywords'),
                'siteemail' => $request->param('siteemail'),
                'sitelogo' => $request->param('sitelogo'),
            ];
            $res= ConfigService::updateConfig($data);
            return json($res);
        }
        $fields = ['sitename','siteurl','siteinfo','sitekeywords','siteemail','sitelogo'];
        $config = ConfigService::getConfig($fields)['data'];
        return View::fetch('index',[
            'config' => $config
        ]);
    }

    /**
     * 扩展配置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-29
     * @param Request $request
     * @return void
     */
    public function extend(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $res= ConfigService::updateConfig($params);
            return json($res);
        }
        $list = ConfigService::getConfigField()['data'];
        $config = ConfigService::getConfig()['data'];
        $configMap = [];
        foreach ($list as &$item) {
            $configMap[$item['fieldname']] = $config[$item['fieldname']];
        }
        return View::fetch('extend',[
            'list' => $list,
            'configMap' => $configMap
        ]);
    }

    /**
     * 添加扩展配置
     */
    public function addExtend(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $res = ConfigService::addOrEditData($params);
            return json($res);
        }
        return View::fetch('addExtend');
    }

    /**
     * 编辑扩展配置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-29
     * @param Request $request
     * @return void
     */
    public function editExtend(Request $request){
        if($request->isPost()){
            $params = $request->param();
            $res = ConfigService::addOrEditData($params);
            return json($res);
        }
        $fid = $request->param('fid',0);
        $detail = ConfigService::configFieldData($fid)['data'];
        return View::fetch('editExtend',[
            'detail' => $detail
        ]);
    }

    /**
     * 删除扩展配置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-29
     * @param Request $request
     * @return void
     */
    public function deleteExtend(Request $request){
        $fid = $request->param('fid',0);
        $res = ConfigService::deleteExtend($fid);
        return json($res);
    }
}
